package com.jinzhi.jzapi.common.controller;

import com.jinzhi.common.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


@Slf4j
@RestController
@RequestMapping("/jzApi/cerUtil")
public class ApiCerUtilController extends BaseController {
    /**
     *合成图片
     *
     * @param backgroundPath
     * @param qrCodePath
     * @param message
     * @param outPutPath
     * @throws IOException
     *      
     */
    public static void overlapImage(String backgroundPath, String qrCodePath, String message, String outPutPath) throws IOException {
        // 设置背景图片大小
        BufferedImage backgroundImage = resizeImage(566, 230, ImageIO.read(new File(backgroundPath)));
        // 设置二维码图片大小
        BufferedImage qrCodeImage = resizeImage(150, 150, ImageIO.read(new File(qrCodePath)));

        Graphics2D graphics = backgroundImage.createGraphics();

        // 在背景图片上添加文字
        graphics.setColor(Color.white);
        graphics.setFont(new Font("楷体", Font.BOLD, 20));
        graphics.drawString(message, 100, 40);

        // 在背景图片上添加二维码图片
        graphics.drawImage(qrCodeImage, 210, 60, qrCodeImage.getWidth(), qrCodeImage.getHeight(), null);
        graphics.dispose();

        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));
    }

    /**
     * 重新设置图片大小
     *
     * @param width
     * @param height
     * @param bufferedImage
     * @return
     *      
     */
    private static BufferedImage resizeImage(int width, int height, BufferedImage bufferedImage) {
        BufferedImage newBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        newBufferedImage.getGraphics().drawImage(bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
        return newBufferedImage;
    }

//    public String cerSynthesis(int type, Map map){
//        String backgroundPath;String eleSealPath;String headPortrait;String outPutPath;
//    }


}
